﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DRentCar
    {
        public bool PhoneNumberExists(string phoneNumber)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tRentCar");
            strSql.Append(" where PhoneNumber=@PhoneNumber");
            MySqlParameter[] parameters = {
					new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,20)
          };
            parameters[0].Value = phoneNumber;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            int result = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            return result > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.RentCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tRentCar(");
            strSql.Append("CityId,PhoneNumber,DriverName,CarNo,CarType)");
            strSql.Append(" values (");
            strSql.Append("@CityId,@PhoneNumber,@DriverName,@CarNo,@CarType)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CityId", MySqlDbType.Int32),
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12),
                    new MySqlParameter("@DriverName", MySqlDbType.VarChar,10),
                    new MySqlParameter("@CarNo", MySqlDbType.VarChar,8),
                    new MySqlParameter("@CarType", MySqlDbType.VarChar,15)                    
                                          };
            parameters[0].Value = model.CityId;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.DriverName;
            parameters[3].Value = model.CarNo;
            parameters[4].Value = model.CarType;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(Model.RentCarInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tRentCar set ");
            strSql.Append("CityId=@CityId,");
            strSql.Append("PhoneNumber=@PhoneNumber,");
            strSql.Append("DriverName=@DriverName,");
            strSql.Append("CarNo=@CarNo,");
            strSql.Append("CarType=@CarType");

            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@CityId", MySqlDbType.Int32),			 			 
                    new MySqlParameter("@PhoneNumber", MySqlDbType.VarChar,12),
                    new MySqlParameter("@DriverName", MySqlDbType.VarChar,10),
                    new MySqlParameter("@CarNo", MySqlDbType.VarChar,8),
                    new MySqlParameter("@CarType", MySqlDbType.VarChar,15),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.CityId;
            parameters[1].Value = model.PhoneNumber;
            parameters[2].Value = model.DriverName;
            parameters[3].Value = model.CarNo;
            parameters[4].Value = model.CarType;
            parameters[5].Value = model.Id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tRentCar ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
            parameters[0].Value = id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.RentCarInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tRentCar ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.RentCarInfo model = new Model.RentCarInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CityId"] != null && ds.Tables[0].Rows[0]["CityId"].ToString() != "")
                {
                    model.CityId = int.Parse(ds.Tables[0].Rows[0]["CityId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PhoneNumber"] != null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString() != "")
                {
                    model.PhoneNumber = ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DriverName"] != null && ds.Tables[0].Rows[0]["DriverName"].ToString() != "")
                {
                    model.DriverName = ds.Tables[0].Rows[0]["DriverName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CarNo"] != null && ds.Tables[0].Rows[0]["CarNo"].ToString() != "")
                {
                    model.CarNo = ds.Tables[0].Rows[0]["CarNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CarType"] != null && ds.Tables[0].Rows[0]["CarType"].ToString() != "")
                {
                    model.CarType = ds.Tables[0].Rows[0]["CarType"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
         
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM tRentCar");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }
    }
}
